package com.wolfpire.workflow.cmd;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.jbpm.api.JbpmException;
import org.jbpm.api.cmd.Environment;
import org.jbpm.pvm.internal.cmd.AbstractCommand;
import org.jbpm.pvm.internal.task.ParticipationImpl;

/**
 * 更新单条参与者记录
 * @author zhenghuazhen
 *
 */
public class GetParticipantByUserIdAndTaskCmd extends AbstractCommand<ParticipationImpl>
{
  private static final long serialVersionUID = 1L;
  protected String taskId;
  protected String userId;

  public GetParticipantByUserIdAndTaskCmd(String taskId, String userId)
  {
    if (taskId == null) {
      throw new JbpmException("taskId is null");
    }
    this.taskId = taskId;
    this.userId = userId;
  }

  public ParticipationImpl execute(Environment environment) throws Exception {
    Criteria criteria = ((Session)environment.get(Session.class)).createCriteria(ParticipationImpl.class);
    //TaskImpl taskImpl=null;
    if (this.taskId != null)
      criteria.add(Restrictions.eq("task.dbid", Long.valueOf(Long.parseLong(this.taskId))));
    if (this.userId != null)
      criteria.add(Restrictions.eq("userId", String.valueOf(this.userId)));
    else {
      throw new JbpmException("no task nor participation specified");
    }

    ParticipationImpl participation = (ParticipationImpl)criteria.uniqueResult();
    return participation;
  }
}